/// <reference path="../../../../../../typings/angularjs/angular.d.ts"/>
define(['./../initial'],
    function () {

        var app = angular.module('App.home');

        app.factory('StatisticInputItem', function () {
            var StatisticInputItem = function (type, min, max, unit) {
                this.type = type;
                switch (type) {
                    case 'less':
                        this.max = max || 3;
                        this.min = 0;
                        break;
                    case 'between':
                        this.max = max || 5;
                        this.min = min || 3;
                        break;
                    case 'more':
                        this.min = min || 10;
                        this.max = 99999;
                        break;
                    default:
                        break;
                }
                this.text = getString(this.type, this.min, this.max, unit);
            };
            StatisticInputItem.prototype.toString = function (unit) {
                var _this = this;
                _this.text = getString(_this.type, _this.min, _this.max, unit);
                return _this.text;
            };
            function getString(type, min, max, unit) {
                unit = unit || "cm";
                if (type === 'less') {
                    return '< ' + max + ' ' + unit;
                } else if (type === 'between') {
                    return min + ' ~ ' + max + ' ' + unit;
                } else {
                    return '> ' + min + ' ' + unit;
                }
            };

            return StatisticInputItem;
        });

        app.factory('statisticInputItemList', ['StatisticInputItem', function (StatisticInputItem) {
            var obj = {
                conditions: [],
                sort: function () {
                    for (var i = 0; i < obj.conditions.length - 1; i++) {
                        var iItem = obj.conditions[i];
                        var tempIndex = i;
                        for (var j = i + 1; j < obj.conditions.length; j++) {
                            var jItem = obj.conditions[j];
                            if (iItem.min > jItem.min) {
                                var temp = obj.conditions[j];
                                obj.conditions[j] = iItem;
                                obj.conditions[tempIndex] = temp;
                                tempIndex = j;
                            }
                        }
                    }
                },
                searchType: 'design',
                addCondition: function (type, min, max) {
                    var unit = this.searchType === 'design' ? 'cm' : 'mm';
                    this.conditions.push(new StatisticInputItem(type, min, max, unit));
                    this.sort();
                    // this.conditions.sort(function (a, b) {
                    //     return a.min > b.min ? 1 : -1;
                    // });
                    window.localStorage.setItem('conditions', JSON.stringify(obj.conditions));
                },
                removeCondition: function (index) {
                    this.conditions.splice(index, 1);
                    window.localStorage.setItem('conditions', JSON.stringify(obj.conditions));
                },
                changeUnit: function () {
                    var unit = this.searchType === 'design' ? 'cm' : 'mm';
                    obj.conditions.forEach(function (item) {
                        item.toString(unit);
                    });
                },
                init: function () {
                    if (obj.conditions.length === 0) {
                        var local = window.localStorage.getItem('conditions');
                        if (local) {
                            obj.conditions = JSON.parse(local).map(function (item) {
                                return new StatisticInputItem(item.type, item.min, item.max);
                            });
                        } else {
                            initConditions();
                        }
                    }
                    obj.sort();
                    return obj.conditions;
                }
            };
            //初始化默认查询条件
            function initConditions() {
                obj.conditions = [];
                var c1 = new StatisticInputItem('less');
                var c2 = new StatisticInputItem('between', 3, 5);
                var c3 = new StatisticInputItem('between', 5, 6);
                var c4 = new StatisticInputItem('between', 6, 10);
                var c5 = new StatisticInputItem('more', 10);
                obj.conditions.push(c1);
                obj.conditions.push(c2);
                obj.conditions.push(c3);
                obj.conditions.push(c4);
                obj.conditions.push(c5);
            }
            obj.init();
            return obj;
        }]);
    });